<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="./utils/createDraw.js"></script>
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
    <script src="/utils/setCanvasStyle.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #btn {
            width: 200px;
            height: 50px;
            z-index: 100;
            position: fixed;
            left: 50px;
            top: 11px;
            background-color: #6528e0;
            border: none;
            border-radius: 15px;
            color: #fff;
        }

        #btn:hover {
            cursor: pointer;
            background-color: #6528e0a7;
        }
    </style>
</head>

<body>
    <button id="btn" onclick="activeDraw()">空间分析</button>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        /* 1、加载geojson数据
        features
         */
        var btn = document.getElementById("btn");
        var source = new ol.source.Vector({
            format: new ol.format.GeoJSON(),
            // geojson格式化器（把它格式化成feature或features的形式）
            // new ol.Feature()或Array<new ol.Feature()>
            url: "/data/query.json"
        })
        var layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer);
        /* 2、点击事件的时候--交互式画布
        获取坐标范围
         */
        let draw_source = new ol.source.Vector({

        })
        let draw_layer = new ol.layer.Vector({
            source: draw_source
        })
        map.addLayer(draw_layer)
        /* high_style */
        // const layers = map.getLayers()
        // console.log(layers,'layers') 
        let high_style = setCanvasStyle(map, 20, 0.35)// map canvas_size speed(单位px/20毫秒)
        function activeDraw() {
            // console.log(btn)
            btn.disabled = true;
            btn.style.cursor = "no-drop"
            let draw = createDraw({
                type: "Box",
                source: draw_source
            })
            map.addInteraction(draw);
            draw.on("drawend", (evt) => {
                draw_source.clear()
                /* 清除画笔 */
                map.removeInteraction(draw);
                /* 获取所画的Box矩形要素的坐标 */
                var position = evt.feature.getGeometry().getCoordinates();
                /* 3、判断有哪些要素在画布的范围内*/
                var polygon = turf.polygon(position);
                // 将三维坐标数组，转换为一个完整的geojson的单个feature
                // console.log(polygon,'polygon')// 一个单个的geojson的feature对象
                /* 获取geojson要素 */
                // var geo_features =source.getFeatures();
                // console.log(geo_features)
                console.log(source, 'source')
                source.forEachFeature(feature => {
                    var center = feature.getGeometry().getCoordinates();
                    /* turf  -geojson */
                    var point = turf.point(center);
                    // console.log(point)
                    /* booleanContais(polygon,point) */
                    var result = turf.booleanContains(polygon, point);
                    if (result) {
                        feature.setStyle(high_style)
                        // console.log(feature)
                    } else {
                        feature.setStyle(null)
                    }
                })

                btn.disabled = false;
                btn.style.cursor = "default"
            })
        }

    </script>
</body>

</html>